package com.ruoyi.sysem.util;


import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ImageWatermarkUtils {

    // 水印横向位置
    private static int positionWidth;
    // 水印纵向位置
    private static int positionHeight;


    /**
     * 给图片添加水印图片、可设置水印图片旋转角度
     * @Param iconPath 水印在服务器或者本地的位置
     * @param srcFile 要添加的水印的文件 可以配合上面的代码一起使用
     * @param degree 水印图片旋转角度 可以不写直接给null  看个人需求
     */
    public static MultipartFile markImageByIcon(String iconPath, MultipartFile srcFile, Integer degree) {
        InputStream is = null;
        OutputStream os = null;
        try {
            InputStream inputImg = srcFile.getInputStream();
            Image srcImg = ImageIO.read(inputImg);
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
            // 1、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 2、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            // 3、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
            }
            // 4、水印图片的路径 水印图片一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);
            // 5、得到Image对象。
            Image img = imgIcon.getImage();
            positionWidth = Math.max(srcImg.getWidth(null) - imgIcon.getIconWidth(), 0);
            positionHeight = Math.max(srcImg.getHeight(null) - imgIcon.getIconHeight(), 0);
            g.drawImage(img, positionWidth, positionHeight, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            // 7、释放资源
            g.dispose();
            // 8、生成图片
            // 获取图片文件名 xxx.png xxx
            String originFileName = srcFile.getOriginalFilename();
            // 获取原图片后缀 png
            int lastSplit = originFileName.lastIndexOf(".");
            String suffix = originFileName.substring(lastSplit + 1);
            // 获取图片原始信息
            String dOriginFileName = srcFile.getOriginalFilename();
            String dContentType = srcFile.getContentType();
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(buffImg, suffix, imOut);
            is = new ByteArrayInputStream(bs.toByteArray());
            // 加水印后的文件上传
            srcFile = new MockMultipartFile(dOriginFileName, dOriginFileName, dContentType,
                    is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return srcFile;
    }


}
